package com.bruce.cloud.feign.client;

import com.bruce.cloud.feign.api.Pageable;
import com.bruce.cloud.feign.api.Store;
import com.bruce.cloud.feign.api.StoreClient;
import jakarta.servlet.http.HttpServletRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by bruce in 2025/6/17 02:19
 */
@RestController
public class ClientController {
    private static final Logger log = LoggerFactory.getLogger(ClientController.class);

    private final StoreClient storeClient;
    private final ClientFactory clientFactory;

    public ClientController(StoreClient storeClient, ClientFactory clientFactory) {
        this.storeClient = storeClient;
        this.clientFactory = clientFactory;
    }

    @RequestMapping("/getStore")
    public Object getStore(HttpServletRequest request) {
        List<Store> stores = storeClient.getStores();
        return stores;
    }

    @RequestMapping("/getStore2")
    public Object getStore2(HttpServletRequest request) {
        StoreClient storeClient2 = clientFactory.create(StoreClient.class);
        List<Store> stores = storeClient2.getStores();
        return stores;
    }

    @RequestMapping("/queryStores")
    public Object queryStores() {
        Pageable pageable = new Pageable();
        pageable.setPage(1);
        pageable.setSize(10);

        return storeClient.queryStores(pageable);
    }

    @RequestMapping("/queryStores2")
    public Object queryStores2() {
        Pageable pageable = new Pageable();
        pageable.setPage(1);
        pageable.setSize(10);

        StoreClient storeClient2 = clientFactory.create(StoreClient.class);
        return storeClient2.queryStores(pageable);
    }

    @RequestMapping("/saveForm")
    public Object saveForm(HttpServletRequest request) {
        Store store = new Store();
        store.setName("saveForm");
        store.setData("哈哈");
        return storeClient.saveForm(store);
    }

    @RequestMapping("/saveForm2")
    public Object saveForm2(HttpServletRequest request) {
        Store store = new Store();
        store.setName("saveForm");
        store.setData("哈哈");

        StoreClient storeClient2 = clientFactory.create(StoreClient.class);
        return storeClient2.saveForm(store);
    }

    @PostMapping("/map")
    public Object map(@RequestParam Map<String, String> params, String age, HttpServletRequest request) {
        Map<String, String[]> parameterMap = request.getParameterMap();
        log.info("map params:{}", params);

        HashMap<String, String> param = new HashMap<>();
        param.put("name", "map");
        param.put("age", "18");
        return storeClient.requestMap(param);
    }

    @PostMapping("/map2")
    public Object map2(Map<String, String> params) {
        log.info("map2 params:{}", params);

        HashMap<String, String> param = new HashMap<>();
        param.put("name", "map");
        param.put("age", "18");

        StoreClient storeClient2 = clientFactory.create(StoreClient.class);
        return storeClient2.requestMap(param);
    }


}
